using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WindowsLive.Writer.Plugin.TimeSpanPlugin.Properties;
using System.Collections;

namespace WindowsLive.Writer.Plugin.TimeSpanPlugin
{
    /// <summary>
    /// Form used by user for :
    /// - Inserting a new Time Span in blog entry
    /// - Modify options (define default values)
    /// </summary>
    public partial class InputForm : Form
    {
        #region Constructors
        public InputForm()
        {
            OptionsMode = false;
            InitializeComponent();
        }

        public InputForm(bool optionsMode)
        {
            OptionsMode = optionsMode;
            InitializeComponent();
        } 
        #endregion

        #region Properties
        private bool optionsMode;
        public bool OptionsMode
        {
            get { return optionsMode; }
            set { optionsMode = value; }
        }
	
        public string OutputFormat
        {
            get { return txtFormat.Text.Trim(); }
            set { txtFormat.Text = value; }
        }

        public DateTime StartDate
        {
            get { return pickStartDate.Value; }
            set { pickStartDate.Value = value; }
        }

        public DateTime EndDate
        {
            get { return pickEndDate.Value; }
            set { pickEndDate.Value = value; }
        }

        public TimeIntervalEnum TimeInterval
        {
            get { return (TimeIntervalEnum)cmbInterval.SelectedItem; }
        }
        #endregion

        /// <summary>
        /// Save default values to User Settings
        /// </summary>
        private void SaveSettings()
        {
            Settings.Default.UseCurrentPostStartDate = cbStart.Checked;
            if(!cbStart.Checked)
                Settings.Default.StartDate = StartDate;
            Settings.Default.UseCurrentPostEndDate = cbEnd.Checked;
            if(!cbEnd.Checked)
                Settings.Default.EndDate = EndDate;
            Settings.Default.OutputFormat = OutputFormat;
            Settings.Default.TimeInterval = (int)TimeInterval;
        }

        /// <summary>
        /// Load default values from User Settings
        /// </summary>
        private void LoadSettings()
        {
            cbStart.Checked = Settings.Default.UseCurrentPostStartDate;
            if(cbStart.Checked)
                StartDate = DateTime.Now;
            else
                StartDate = Settings.Default.StartDate;

            cbEnd.Checked = Settings.Default.UseCurrentPostEndDate;
            if(cbEnd.Checked)
                EndDate = DateTime.Now;
            else
                EndDate = Settings.Default.EndDate;

            OutputFormat = Settings.Default.OutputFormat;
            cmbInterval.SelectedIndex = cmbInterval.Items.IndexOf((TimeIntervalEnum)Settings.Default.TimeInterval);
        }

        private void InputForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Save form location
            Settings.Default.InputFormLocation = this.Location;
            Settings.Default.Save();

            // Save settings (if required)
            if(cbSaveAsDefault.Checked)
                SaveSettings();
        }

        private void InputForm_Load(object sender, EventArgs e)
        {
            // Move form to the previous location
            if (!Settings.Default.InputFormLocation.IsEmpty)
                this.Location = Settings.Default.InputFormLocation;

            // Bind list of time interval
            cmbInterval.DataSource = GetListTimeInterval();

            // Load user settings
            LoadSettings();

            // Change display
            ChangeDisplayAccordingToMode(OptionsMode);
        }

        private void ChangeDisplayAccordingToMode(bool displayAsOption)
        {
            cbSaveAsDefault.Visible = !displayAsOption;
            cbSaveAsDefault.Checked = displayAsOption;
        }

        /// <summary>
        /// Get the list of time interval to bind the ComboBox
        /// </summary>
        private IList GetListTimeInterval()
        {
            Type intervalType = typeof(TimeIntervalEnum);
            IList lstTimeInterval = new ArrayList();
            foreach (TimeIntervalEnum interval in Enum.GetValues(intervalType))
                lstTimeInterval.Add(interval);
            return lstTimeInterval;
        }

        /// <summary>
        /// CheckBox "use current post start date"
        /// </summary>
        private void cbStart_CheckedChanged(object sender, EventArgs e)
        {
            CheckChanged(sender as CheckBox, pickStartDate);
            if (cbStart.Checked)
                cbEnd.Checked = false;
        }

        /// <summary>
        /// CheckBox "use current post end date"
        /// </summary>
        private void cbEnd_CheckedChanged(object sender, EventArgs e)
        {
            CheckChanged(sender as CheckBox, pickEndDate);
            if (cbEnd.Checked)
                cbStart.Checked = false;
        }

        /// <summary>
        /// Change picker date and state according to CheckBox checked state
        /// </summary>
        private void CheckChanged(CheckBox chk, DateTimePicker timePicker)
        {
            // The corresponding checked timepicker receive the current date
            if(chk.Checked)
                timePicker.Value = DateTime.Now;

            // If checked it is disabled
            timePicker.Enabled = !chk.Checked;
        }

        /// <summary>
        /// Invert start date and end date
        /// </summary>
        private void lnkInvertDates_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DateTime tempDate = pickStartDate.Value;
            pickStartDate.Value = pickEndDate.Value;
            pickEndDate.Value = tempDate;

            if (cbStart.Checked)
                cbEnd.Checked = true;
            else
                if (cbEnd.Checked)
                    cbStart.Checked = true;
        }
    }
}